﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SquidConsult.BLL;
using System.IO;

namespace SquidConsult.UI.Supervisor
{
  public partial class _default : System.Web.UI.Page
  {
      MembershipUser userID;
      private SupervisorBL supervisor = new SupervisorBL();

      #region Load Page
      protected void Page_Load(object sender, EventArgs e)
    {
        userID = Membership.GetUser();
        LoadBudget();
        LoadReport();
    }

    private void LoadReport()
    {
        try
        {
            if (ReportDropDownList.SelectedIndex == 0)
                ReportGridView.DataSource = supervisor.GetAllReport(userID.ToString());
            else
                ReportGridView.DataSource = supervisor.GetRejectedReport(userID.ToString());

            ReportGridView.DataBind();
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "Reports load fail : The following error occured: " + ex.Message;
        }
    }

    private void LoadBudget()
    {
        try
        {
            ExpenseLabel.Text = supervisor.GetDepartmentTotalExpense(userID.ToString()).ToString();
            BudgetLabel.Text = supervisor.GetRemainBuget(userID.ToString()).ToString();
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "Budget load fail : The following error occured: " + ex.Message;
        }
    }
      #endregion

      #region Approve or project Report
    protected void ReportDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadReport(); 
    }

    protected void ReportGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ExpenseGridView.DataSource = supervisor.GetExpense(Guid.Parse(ReportGridView.SelectedRow.Cells[1].Text));
            ExpenseGridView.DataBind();
            UpdateVisible();
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "Expense load fail : The following error occured: " + ex.Message;
        }
    }

    private void UpdateVisible()
    {
        UpdateStatusPanel.Visible = true;
        Sup_StatusDDL.SelectedValue = ReportGridView.SelectedRow.Cells[5].Text;
        //double tempTotalExpense = double.Parse(ExpenseLabel.Text) - double.Parse(ReportGridView.SelectedRow.Cells[4].Text);
        //TotalExpenseLabel.Text = tempTotalExpense.ToString();
    }
    
    protected void ExpenseGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        DLPanel.Visible = true;
    }

    protected void UpdateStatusButton_Click(object sender, EventArgs e)
    {
        try
        {
            supervisor.UpdateStatus(Guid.Parse(ReportGridView.SelectedRow.Cells[1].Text), Sup_StatusDDL.SelectedValue.ToString());
            LoadReport();
            LoadBudget();
            checkBudget();
        }
        catch (Exception ex)
        {
            ErrorLabel.Text = "Report update fail : The following error occured: " + ex.Message;
        }
    }

    private void checkBudget()
    {
        double tempBudget = double.Parse(BudgetLabel.Text) - double.Parse(ReportGridView.SelectedRow.Cells[4].Text);
        if (tempBudget < 0 && Sup_StatusDDL.SelectedIndex == 2)
            ErrorLabel.Text = "The department budget is going over";
        else
            ErrorLabel.Text = "";
    }

    #endregion

      #region Download Receipt
    protected void DLButton_Click(object sender, EventArgs e)
    {
        try
        {
            string receiptPath = supervisor.GetReceiptPath(Guid.Parse(ExpenseGridView.SelectedRow.Cells[1].Text));
            FileStream fileStream = new FileStream(receiptPath, FileMode.Open, FileAccess.Read);
            Byte[] binaryData = new Byte[fileStream.Length];
            fileStream.Read(binaryData, 0, (Int32)fileStream.Length);
            fileStream.Close();
            Response.ContentType = "pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=Receipt.pdf");
            Response.AddHeader("Content-length", binaryData.Length.ToString());
            Response.BinaryWrite(binaryData);
            Response.Flush();
            Response.Clear();
        }
        catch (System.IO.FileNotFoundException)
        {
            DLErrorLabel.Text = "The receipt does not exist";
        }
    }
    #endregion 

    protected void ReportGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //make reportId and expenseId invisible 
        e.Row.Cells[1].CssClass = "hiddencol";
    }


  }
}